package org.wdcode.web.filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.wdcode.common.constants.StringConstants;
import org.wdcode.common.util.CommonUtil;

import java.io.IOException;

/**
 * 检测用户session值
 * @author WD
 * @since JDK6
 * @version 1.0 2009-03-01
 */
public final class SessionFilter implements Filter {
	// 常量
	private final static String	FORWARD;
	// 是否进行SESSION检查
	private boolean				sessionCheck;
	// 检查session的key值
	private String				sessionKey;
	// 检查为空时跳到的错误页
	private String				errorPage;
	// 不检查的页面
	private String				special;
	// 跳转方式
	private String				resultType;

	// 静态初始化
	static {
		FORWARD = "forward";
	}

	/**
	 * 载入过滤器到服务 读取过滤器配置参数
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		// 获得是否检查session
		String check = filterConfig.getInitParameter("sessionCheck");
		sessionCheck = CommonUtil.isEmpty(check) ? true : Boolean.parseBoolean(check);

		// 设置检查session的key值
		sessionKey = filterConfig.getInitParameter("userKey");
		if (CommonUtil.isEmpty(sessionKey)) {
			this.sessionKey = "user";
		}

		// 设置错误页
		errorPage = filterConfig.getInitParameter("errorPage");
		if (CommonUtil.isEmpty(errorPage)) {
			this.errorPage = "/index.jsp";
		}

		// 设置不检查的页面
		special = filterConfig.getInitParameter("special");

		// 设置跳转方式
		resultType = filterConfig.getInitParameter("resultType");
		if (CommonUtil.isEmpty(resultType)) {
			this.resultType = "redirect";
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
		// 转换Request
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		// 转换Response
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		// 获得session
		HttpSession session = httpRequest.getSession();

		// 获得要检验的session值
		Object user = session.getAttribute(sessionKey);

		// 校验页面
		boolean find = true;
		// 判断有不检查的页面
		if (!CommonUtil.isEmpty(special)) {
			// 获得不检查的页面的数组形式
			String[] exceptPage = special.split(StringConstants.COMMA);
			// 获得当前页面
			String paths = httpRequest.getServletPath();
			// 有数组
			if (exceptPage.length >= 0) {
				// 循环数组
				for (int i = 0; i < exceptPage.length; i++) {
					// 查找是相同页
					if (paths.indexOf(exceptPage[i]) >= 0) {
						// 不检查
						find = false;
						// 跳出循环
						break;
					}
				}
			}
		}

		// 判断检查
		if (sessionCheck && find && CommonUtil.isEmpty(user)) {
			// 判断跳转方式
			if (FORWARD.equals(resultType)) {
				// forward
				httpRequest.getRequestDispatcher(errorPage).forward(httpRequest, httpResponse);
			} else {
				// redirect
				httpResponse.sendRedirect(httpRequest.getContextPath() + errorPage);
			}
		} else {
			// 交出控制权
			filterChain.doFilter(httpRequest, httpResponse);
		}
	}

	/**
	 * 销毁实例调用
	 */
	public void destroy() {}
}
